﻿// -----------------------------------------------------------------------
// <copyright file="HistoryReader.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ClearLines.Stocks.Domain
{
   using System;
   using System.Collections.Generic;
   using System.IO;
   using System.Linq;
   using System.Net;
   using System.Text;

   /// <summary>
   /// TODO: Update summary.
   /// </summary>
   public class HistoryReader
   {
      public static StockHistory Read(string symbol, DateTime startDate, DateTime endDate)
      {
         var query = String.Format("&a={0}&b={1}&c={2}&d={3}&e={4}&f={5}&g=d&ignore=.csv",
                                   startDate.Month - 1, startDate.Day, startDate.Year, 
                                   endDate.Month - 1, endDate.Day, endDate.Year);

         var url = "http://ichart.finance.yahoo.com/table.csv?s=" + symbol + query;

         var request = WebRequest.Create(url);

         using (var response = request.GetResponse())
         {            
            using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
            {
               var result = reader.ReadToEnd();
               var resultByDay = result.Split((char)10);
               var history = new List<TradingDaySummary>();
               for (var line = 1; line < resultByDay.Count() - 1; line++)
               {
                  var summary = ParseTradingDaySummary(resultByDay[line]);
                  history.Add(summary);
               }

               return new StockHistory(symbol, startDate, endDate, history);
            }
         }
      }

      private static TradingDaySummary ParseTradingDaySummary(string dayData)
      {
         var split = dayData.Split(',');

         var summary = new TradingDaySummary();
         summary.Day = Convert.ToDateTime(split[0]);
         summary.Open = Convert.ToDecimal(split[1]);
         summary.High = Convert.ToDecimal(split[2]);
         summary.Low = Convert.ToDecimal(split[3]);
         summary.Close = Convert.ToDecimal(split[4]);
         summary.Volume = Convert.ToInt64(split[5]);

         return summary;
      }

      // Backup method for demo, in case there is no internet access.
      private static StockHistory ReadFromTextFile()
      {
         var fileLocation = @"C:\Users\Mathias Brandewinder\Desktop\MSFT.txt";
         using (var reader = new StreamReader(fileLocation, Encoding.ASCII))
         {
            var result = reader.ReadToEnd();
            var resultByDay = result.Split((char)10);
            var history = new List<TradingDaySummary>();
            for (var line = 1; line < resultByDay.Count() - 1; line++)
            {
               var summary = ParseTradingDaySummary(resultByDay[line]);
               history.Add(summary);
            }

            return new StockHistory("MSFT", new DateTime(2011, 1, 1), new DateTime(2012, 1, 1), history);
         }
      }
   }
}
